﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using WangMi.DAL;
using WangMi.Model;
using System.Text.RegularExpressions;

namespace WangMi.BLL
{
    public class BLL_Menu
    {
        public static List<Menu> GetMenuXml()
        {
            string basePath = AppDomain.CurrentDomain.BaseDirectory;
            string configPath = basePath + "Configs/Menu.xml";

            DAL_Menu.GetAllMenu();

            List<Menu> menuList = new List<Menu>();
            XElement root = XElement.Load(configPath);
            IEnumerable<XElement> xml =
                from el in root.Elements("Menu")
                select el;

            foreach (XElement item in xml)
            {
                Menu menu = new Menu();
                menu.Id = item.Attribute("Id").Value;
                menu.MenuName = item.Attribute("MenuName").Value;
                menuList.Add(menu);
            }

            return menuList;
        }

        public static List<SubMenu> GetSubMenu(int menuId)
        {
            string basePath = AppDomain.CurrentDomain.BaseDirectory;
            string configPath = basePath + "Configs/Menu.xml";

            List<SubMenu> subMenuList = new List<SubMenu>();
            XElement root = XElement.Load(configPath);
            IEnumerable<XElement> xml =
                from el in root.Elements("Menu")
                where (string)el.Attribute("Id") == menuId.ToString()
                select el;

            string controllerName = xml.Attributes("ControllerName").ElementAtOrDefault(0).Value;

            foreach (XElement item in xml.Nodes())
            {
                SubMenu subMenu = new SubMenu();
                subMenu.Id = item.Attribute("Id").Value;
                subMenu.ActionName = item.Attribute("ActionName").Value;
                subMenu.ControllerName = controllerName;
                subMenu.SubMenuName = item.Attribute("SubMenuName").Value;
                subMenuList.Add(subMenu);
            }
            return subMenuList;
        }
    }
}